package studio.banner.reference.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;
import studio.banner.reference.entity.Aggregate;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class HtmlController extends BaseController {

    @GetMapping({"/", "/index"})
    public ModelAndView index(Model model) {
        ModelAndView modelAndView = new ModelAndView("index");
        return modelAndView;
    }

    @GetMapping("/import")
    public String importHtml() {
        return "/visitor/import";
    }

    @GetMapping("/home")
    public ModelAndView homeHtml(HttpServletRequest request) {
        Integer userId = getUserIdBySession(request.getSession());
        ModelAndView mv = new ModelAndView("home");
        List<Aggregate> thesisList = aggregateService.listByAggregateType("thesis", userId);
        List<Aggregate> subjectList = aggregateService.listByAggregateType("subject", userId);
        mv.addObject("thesisList", thesisList);
        mv.addObject("subjectList", subjectList);
        Aggregate aggregate = new Aggregate();
        if (thesisList.size() > 0) {
            aggregate = thesisList.get(0);
        }
        mv.addObject("aggregate", aggregate);
        return mv;
    }

    @GetMapping("/login")
    public String loginHtml() {
        return "login";
    }

    @GetMapping("/aggregate/list")
    public String aggregateList(Model model, HttpSession session) {
        Integer userId = getUserIdBySession(session);
        List<Aggregate> thesisList = aggregateService.listByAggregateType("thesis", userId);
        List<Aggregate> subjectList = aggregateService.listByAggregateType("subject", userId);
        model.addAttribute("thesisList", thesisList);
        model.addAttribute("subjectList", subjectList);
        Aggregate aggregate = (thesisList.size() > 0 ? thesisList.get(0) : new Aggregate());
        model.addAttribute("aggregate", aggregate);
        return "unit/slider-bar-table :: slider-bar-table";
    }

    @GetMapping("/aggregate/dataTable")
    public String aggregateDataTable(Integer aggregateId, Model model) {
        Aggregate aggregate = aggregateService.getById(aggregateId);
        model.addAttribute("aggregate", aggregate);
        return "/unit/home-table::list-agreegate";
    }

    @GetMapping("/register")
    public String registerHtml() {
        return "register";
    }

}
